classdef MarkerTransformInfo
    %MARKERTRANSFORMINFO
    
    properties
        OriginalMarkerNumber = 0;
        LatencySeconds = 0
        LatencyFrames = 0;
        OriginalType = '';
        Action = 'None';
        NewType = '';
    end
    
    methods
        
        function mi = MarkerTransformInfo(originalMarkerNumber, latencySeconds, latencyFrames, originalType)
            if nargin == 4
                mi.OriginalMarkerNumber = originalMarkerNumber;
                mi.LatencySeconds = latencySeconds;
                mi.LatencyFrames = latencyFrames;
                mi.OriginalType = originalType;
            end
        end % MarkerTransformInfo
        
        function obj = set.Action(obj,action)
            if ~(ischar(action))
                error('Action must be a string')
            end
            if ~( strcmp(action, 'None') || strcmp(action, 'Rename') || strcmp(action, 'Delete') )
                error('Action must be either ''None'', ''Rename'' or ''Delete''.')
            end
            obj.Action = action;
        end % Name set function
        
    end
    
    methods(Static)
        
        function [markerTransforms] = CreateMarkerTransformInfo( eeglabEvent, eeglabSrate )
            % Create the marker transform
            for i = 1:length(eeglabEvent)
                markerTransforms(i) = MarkerTransformInfo(i, eeglabEvent(i).latency / eeglabSrate, eeglabEvent(i).latency, eeglabEvent(i).type);
            end
        end
        
        function DisplayArray( m )
            {m(:).LatencySeconds; m(:).OriginalType; m(:).Action; m(:).NewType}'
        end
        
        function [ matrix ] = ExportMarkerTransform( markerTransformArray )
            matrix = [ {markerTransformArray(:).OriginalMarkerNumber}', ...
                {markerTransformArray(:).LatencySeconds}', ...
                {markerTransformArray(:).OriginalType}', ...
                {markerTransformArray(:).Action}', ...
                {markerTransformArray(:).NewType}' ];
        end
        
        function [ markerTransformArray ] = ImportMatrix( markerTransformArray, matrix )
            for i = 1:length(matrix)
                if markerTransformArray(i).LatencySeconds == cell2mat(matrix(i,2))
                    markerTransformArray(i).NewType = cell2mat(matrix(i,5));
                    markerTransformArray(i).Action = cell2mat(matrix(i,4));
                else
                    error('Latencies do not match.');
                end
            end
        end
        
    end
    
end